package a07_二叉树;

/**
 * <p>
 * a08_对称二叉树复习2
 * </p>
 *
 * @author flyduck
 * @since 2025/1/14
 */
public class a08_对称二叉树复习2 {
    public boolean isSymmetric(TreeNode root) {
        return compare(root.left, root.right);
    }

    private boolean compare(TreeNode leftNode,TreeNode rightNode){
        if(leftNode == rightNode){
            return true;
        }else if(leftNode != null && rightNode == null){
            return false;
        }else if(leftNode == null && rightNode != null){
            return false;
        }else if(leftNode.val != rightNode.val){
            return false;
        }

        boolean outerCompare = compare(leftNode.left, rightNode.right);
        boolean innerCompare = compare(leftNode.right, rightNode.left);
        return outerCompare && innerCompare;
    }
}
